/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.Imp;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import data.Services.UserDAOService;
import model.entity.User;

/**
 *
 * @author admin
 */
public class UserServiceDAOImp extends ConnectionManager implements UserDAOService {

    private static UserServiceDAOImp userDAO;

    public static UserServiceDAOImp getInstance() {
        if (userDAO == null) {
            userDAO = new UserServiceDAOImp();
        }
        return userDAO;
    }

    private User createNewUser(ResultSet rs) throws SQLException {
        User user = new User();
        user.setAddress(rs.getString("address"));
        user.setBirthday(rs.getDate("birthday"));
        user.setCity(rs.getString("city"));
        user.setEmail(rs.getString("email"));
        user.setFullName(rs.getString("fullname"));
        user.setImage(rs.getString("image"));
        user.setPassword(rs.getString("password"));
        user.setPhone(rs.getString("phone"));
        user.setPoint(rs.getInt("point"));
        user.setRole(rs.getInt("role"));
        user.setStatus(rs.getInt("status"));
        user.setUserID(rs.getInt("user_id"));
        return user;
    }

    @Override
    public List<User> getAllUser() {
        List<User> userList = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "select * from users where status = 1";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                userList.add(createNewUser(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("success!");
        return userList;
    }

    @Override
    public User getUserByID(int id) {
        User user = new User();
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "select * from users where user_id = ? and";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                user = createNewUser(rs);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return user;
    }

    @Override
    public User getUserByEmail(String email) {
        User user = new User();
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "select * from users where email = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                user = createNewUser(rs);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return user;
    }

    @Override
    public List<User> findUserByUserName(String fullname) {
        List<User> userList = new ArrayList<>();
        try {

            Connection conn = ConnectionManager.getConnection();
            String sql = "select * from users where fullname like '%?%' and status = 1";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fullname);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                userList.add(createNewUser(rs));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return userList;
    }

    @Override
    public List<User> findUserByRole(int roleID) {
        List<User> userList = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "select * from users where role = ? and status = 1";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, roleID);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                userList.add(createNewUser(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public boolean createUser(User user) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "insert into users(user_id, fullname, address, city, email, phone , "
                    + "password, point, status) values (?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getUserID());
            pstmt.setString(2, user.getFullName());
            pstmt.setString(3, user.getAddress());
            pstmt.setString(4, user.getCity());
            pstmt.setString(5, user.getEmail());
            pstmt.setString(6, user.getPhone());
            pstmt.setString(7, user.getPassword());
            pstmt.setInt(8, user.getPoint());
            pstmt.setInt(9, user.getStatus());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean updateProfile(User user) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set fullname = ?, address = ?, city = ? , email = ?, "
                    + "phone = ? , birthday = ?, image = ? where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getFullName());
            pstmt.setString(2, user.getAddress());
            pstmt.setString(3, user.getCity());
            pstmt.setString(4, user.getEmail());
            pstmt.setString(5, user.getPhone());
            pstmt.setDate(6, user.getBirthday());
            pstmt.setString(7, user.getImage());
            pstmt.setInt(8, user.getUserID());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return isCheck;
    }

    @Override
    public boolean updatePassword(User user) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set password = ? where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getPassword());
            pstmt.setInt(2, user.getUserID());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean updatePassword(int user_id, String password) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set password = ? where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, password);
            pstmt.setInt(2, user_id);
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean updateUserByAdmin(User user) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set point = ?, role = ?, status = ? where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, user.getPoint());
            pstmt.setInt(2, user.getRole());
            pstmt.setInt(3, user.getStatus());
            pstmt.setInt(4, user.getUserID());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isCheck;
    }

    @Override
    public boolean restoreUser(int userID) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set status=1 where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userID);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteUser(int userID) {
        try {
            Connection conn = ConnectionManager.getConnection();
            String sql = "update users set status=0 where user_id = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userID);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean activeAccount(String name, String activeCode) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getRegisterId(User user) {
        try {
            Connection conn = ConnectionManager.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from users where fullname = '" + user.getFullName()
                    + "' and password = '" + user.getPassword() + "'");
            if (rs.next()) {
                return rs.getInt("user_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean updateStatus(String email, int status) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update users set status = ? where email = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(2, email);
            statement.setInt(1, status);
            int row = statement.executeUpdate();
            if (row > 0) {
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

}
